﻿
var viewModel = new function () {
    // Configs
    this.Keywords = ko.observable("Keywords");
    this.Banners = ko.observableArray(data.ListBannerTransaction);
    this.OptionList = ko.observableArray(data.ListBannerOption);
    this.PaymentList = ko.observableArray(data.ListPaymentMethod);
    this.RangeList = ko.observableArray(data.ListRangeTime);

    this.PageSize = ko.observable(data.Paging.PageSize);
    this.CurrentPageIndex = ko.observable(1);
    this.MaxPageIndex = ko.observable(data.Paging.TotalPages);
    this.ColName = ko.observable("DateAdded");
    this.SortOrder = ko.observable("desc");
    this.BannerId = ko.observable(0);

    // Total Records
    this.TotalRecords = ko.observable(data.Paging.TotalRecords);
    this.PageText = ko.computed(function () {
        var total = this.TotalRecords();
        if (total == 1)
            return total + " giao dịch";
        else
            return total + " giao dịch";

    }, this);

    this.PageSizeText = ko.computed(function () {
        var pagevalue = this.PageSize().toString().replace(/\s/g, '');
        var pagesize = parseInt(pagevalue, 10);
        if (!IsNumeric(pagevalue))
            return "* Nhập một số giữa 1 - 100";
        else if (pagesize.toString() == 'NaN' || pagesize < 1 || pagesize > 100) {
            return "* Nhập một số giữa 1 - 100";
        }
        else {
            return "";
        }

    }, this);
    this.PageCurrentText = ko.computed(function () {
        var pagevalue = this.CurrentPageIndex().toString().replace(/\s/g, '');
        var pageindex = parseInt(pagevalue, 10);

        if (!IsNumeric(this.CurrentPageIndex()) || pagevalue == "")
            return "* Nhập một số giữa 1 - " + this.MaxPageIndex() + " ";
        else if (pagevalue.toString() == 'NaN' || !IsNumeric(pagevalue) || pageindex < 1 || pageindex > this.MaxPageIndex()) {
            return "* Nhập một số giữa 1 - " + this.MaxPageIndex() + " ";
        }
        else {
            return "";
        }

    }, this);

    // Paginate
    this.PrevPage = function () {
        var page = parseInt(this.CurrentPageIndex());
        if (page > 1) {
            this.CurrentPageIndex(page - 1);
            LoadDataFromServer();
        }
    }
    this.PrevButtonClass = ko.computed(function () {
        var page = parseInt(this.CurrentPageIndex());
        if (page > 1) {
            return "btn btn-prev-arrow prev-active";
        }
        else {
            return "btn btn-prev-arrow";
        }
    }, this);

    this.NextPage = function () {
        var page = parseInt(this.CurrentPageIndex());
        if (page < this.MaxPageIndex()) {
            this.CurrentPageIndex(page + 1);
            LoadDataFromServer();
        }
    }
    this.NextButtonClass = ko.computed(function () {
        if (this.CurrentPageIndex() >= this.MaxPageIndex() || this.MaxPageIndex() == 1) {
            return "btn btn-next-arrow next-disabled";
        }
        else {
            return "btn btn-next-arrow";
        }
    }, this);

    this.DisableNextButton = ko.computed(function () {
        if (IsNumeric(this.CurrentPageIndex())) {
            var pageIndex = parseInt(this.CurrentPageIndex(), 10);
            var maxPage = parseInt(this.MaxPageIndex());

            if (pageIndex >= maxPage)
                return 1;
            else
                return 0;
        }
        return 0;
    }, this);

    // Load data
    function LoadDataFromServer() {
        var pagevalue = $("#pagesize").val().toString().replace(/\s/g, '');
        var pagesize = parseInt(pagevalue, 10);
        if (pagesize.toString() == 'NaN' || !IsNumeric(pagesize) || pagesize < 1 || pagesize > 100) {
            viewModel.PageSize(data.PageSize);
        }

        pagevalue = $("#pagecurrent").val().toString().replace(/\s/g, '');
        var pageindex = parseInt(pagevalue, 10);
        if (pageindex.toString() == 'NaN' || !IsNumeric(pageindex) || pageindex < 1 || pageindex > viewModel.MaxPageIndex()) {
            viewModel.CurrentPageIndex(data.PageIndex);
        }
        var optionId = $('#optionSelected').val();
        var paymentId = $('#paymentSelected').val();
        optionId = (optionId != undefined && optionId != "") ? optionId : "0";
        paymentId = (paymentId != undefined && paymentId != "") ? paymentId : "0";

        var startDate = "";
        var endDate = "";
        var rangeDate = $('#RangeSelectedValue').val();
        if (rangeDate != undefined && rangeDate != null && rangeDate != '') {
            rangeArray = rangeDate.split('|');
            startDate = rangeArray[0];
            endDate = rangeArray[1];
        }

        var keywords = $('#Keywords').val();
        if (keywords.toLowerCase() == "keywords")
            keywords = "";

        // Json post
        var url = "/BannerTransactionList/Fillter";
        var data = { "keywords": keywords, "startDate": startDate, "endDate": endDate, "optionId": optionId, "paymentId": paymentId, "pageSize": pagesize, "pageIndex": pageindex, "id": viewModel.BannerId(), "sortBy": viewModel.ColName(), "sortOrder": viewModel.SortOrder() };

        // Json Get function to retrive data from server
        $('.waiting').remove();
        $('#apps').waiting();
        $('.waiting').show();
        $('#progress').css("display", "block");

        $.post(url, data, function (model) {
            if (model != undefined && model != null) {
                viewModel.CurrentPageIndex(model.Paging.PageIndex);
                viewModel.MaxPageIndex(model.Paging.TotalPages);
                viewModel.PageSize(model.Paging.PageSize);
                viewModel.TotalRecords(model.Paging.TotalRecords);
                viewModel.Banners(model.ListBannerTransaction);
            }
            // Stop progress bar
            $('.waiting').hide();
            $('#progress').css("display", "none");

        });
    }

    // Sort by Colum/Order
    function SetOrder(event, ColName) {
        if (this.ColName() === ColName) {
            this.SwitchSortOrder();
        } else {
            this.ColName(ColName);
            this.SortOrder("asc");
        }
        SetTHClass(ColName, event);
        LoadDataFromServer();
    }
    this.SetOrder = SetOrder;
    function SwitchSortOrder() {
        if (this.SortOrder() === "asc") {
            this.SortOrder("desc");
        } else {
            this.SortOrder("asc");
        }
    }
    this.SwitchSortOrder = SwitchSortOrder;

    function SetTHClass(colName, event) {
        $("div.title").parent().find(".sort_wrapper").children("span").removeClass("asc");
        $("div.title").parent().find(".sort_wrapper").children("span").removeClass("desc");

        if (viewModel.SortOrder() === "asc") {
            $("#" + colName).children("span").addClass("asc");
        } else {
            $("#" + colName).children("span").addClass("desc");
        }
    }

    // Delete view lead items
    this.LockBanner = function () {
        this.BannerId($('#bannerId').val());
        $.fancybox.close();
        LoadDataFromServer();

    };

    // PageSize/ PageCurrent keypress enter
    this.RunSomethingKey = function (viewModel, event) {
        if (event.keyCode == 13 || event.keyCode == 9) {
            viewModel.CurrentPageIndex(1);
            this.RunSomething(viewModel, event);
        }
        return true;
    }

    this.RunSomethingKey2 = function (viewModel, event) {
        if (event.keyCode == 13 || event.keyCode == 9) {
            this.RunSomething(viewModel, event);
        }
        return true;
    }

    this.RunSomething = function (viewModel, event) {
        LoadDataFromServer();
    }

    this.GoFilter = function () {
        LoadDataFromServer();
    }
};

ko.applyBindings(viewModel, document.getElementById("main"));

$(document).ready(function () {
    $(".calendar-date").datepicker();
});

function DeleteDialog(id) {
    $('#bannerId').val(id);
    $('#bannername').text($('#banner' + id).attr("rel"));
}